# coding=utf-8
import logging

from venusdemilo import db_drivers

query_string = ''


def emit(batch_size=None):
    db_driver = db_drivers.MySqlDriver(
        host='localhost', user='root', passwd='root1234', db='scrapydb'
    )

    item_list = _get_items_from_db(db_driver, batch_size)
    # total_count = _get_total_count(db_driver)
    # batch_count = total_count / batch_size + 1
    # for i in range(batch_count):

    return item_list


def _get_total_count(db_driver):
    total_count_result = db_driver.execute_query("select count(1) from scrapydb.t_sinahealth_dump")
    if len(total_count_result) > 0 and len(total_count_result[0]) > 0:
        return total_count_result[0][0]
    else:
        raise Exception("get db total count error... may no data...")


def _get_items_from_db(db_driver, batch_size=None):
    logging.info("getting items from database...")
    if batch_size:
        query_result = db_driver.execute_query_batch(query_string, batch_size)
    else:
        query_result = db_driver.execute_query(query_string)
    return _build_item(query_result)


def _build_item(query_result):
    html_list = [
        {"id": c[0], "title": c[1], "content": c[2], "spider": c[3], "category": c[4], "tag": c[5], "url": c[6],
         "referer": c[7], "issue_date": c[8], "source": c[9], "createDtTm": c[10]} for c in query_result]

    return html_list
